/* 
 * Copyright (C) 2008 anphoralhrod.blogspot.com
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received Oi copy of the GNU Library General Public
 * License along with this library; if not, write to the Free
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 * BBPanel.java
 *
 * Created on 6 de julio de 2008, 10:52
 */

package betcalc;

import betcalc.betCalc.TCalc;

/**
 *
 * @author  fran
 */
public class BLPanel extends javax.swing.JPanel {
    
    /** Creates new form BBPanel */
    public BLPanel() {
        initComponents();
        calculate();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel3 = new javax.swing.JLabel();
        profitSpinner = new javax.swing.JSpinner();
        odd2Label = new javax.swing.JLabel();
        stake2Label = new javax.swing.JLabel();
        lostProfit2Label = new javax.swing.JLabel();
        winProfit2Label = new javax.swing.JLabel();
        total2Label = new javax.swing.JLabel();
        odd1Spinner = new javax.swing.JSpinner();
        stake1Spinner = new javax.swing.JSpinner();
        jLabel1 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        winProfit1Label = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        lostProfit1Label = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        total1Label = new javax.swing.JLabel();
        warningLabel = new javax.swing.JLabel();

        setLayout(null);

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("betcalc/Bundle"); // NOI18N
        jLabel3.setText(bundle.getString("netprofitlabel")); // NOI18N
        add(jLabel3);
        jLabel3.setBounds(10, 0, 60, 20);

        profitSpinner.setModel(new javax.swing.SpinnerNumberModel(Float.valueOf(1.0f), Float.valueOf(0.0f), null, Float.valueOf(0.01f)));
        profitSpinner.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                profitSpinnerStateChanged(evt);
            }
        });
        add(profitSpinner);
        profitSpinner.setBounds(10, 20, 60, 20);

        odd2Label.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        odd2Label.setText("0");
        odd2Label.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        odd2Label.setEnabled(false);
        add(odd2Label);
        odd2Label.setBounds(10, 120, 60, 20);

        stake2Label.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        stake2Label.setText("0");
        stake2Label.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        stake2Label.setEnabled(false);
        add(stake2Label);
        stake2Label.setBounds(90, 120, 60, 20);

        lostProfit2Label.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lostProfit2Label.setText("0");
        lostProfit2Label.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        lostProfit2Label.setEnabled(false);
        add(lostProfit2Label);
        lostProfit2Label.setBounds(250, 120, 60, 20);

        winProfit2Label.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        winProfit2Label.setText("0");
        winProfit2Label.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        winProfit2Label.setEnabled(false);
        add(winProfit2Label);
        winProfit2Label.setBounds(170, 120, 60, 20);

        total2Label.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        total2Label.setText("0");
        total2Label.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        total2Label.setEnabled(false);
        add(total2Label);
        total2Label.setBounds(250, 160, 60, 20);

        odd1Spinner.setModel(new javax.swing.SpinnerNumberModel(Float.valueOf(1.01f), Float.valueOf(1.01f), Float.valueOf(1000.0f), Float.valueOf(0.01f)));
        odd1Spinner.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        odd1Spinner.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                odd1SpinnerStateChanged(evt);
            }
        });
        add(odd1Spinner);
        odd1Spinner.setBounds(10, 70, 59, 20);

        stake1Spinner.setModel(new javax.swing.SpinnerNumberModel(Float.valueOf(2.0f), Float.valueOf(0.01f), Float.valueOf(1000.0f), Float.valueOf(0.01f)));
        stake1Spinner.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                stake1SpinnerStateChanged(evt);
            }
        });
        add(stake1Spinner);
        stake1Spinner.setBounds(90, 70, 63, 20);

        jLabel1.setText(bundle.getString("odd1label")); // NOI18N
        add(jLabel1);
        jLabel1.setBounds(10, 50, 60, 20);

        jLabel4.setText(bundle.getString("stakeLabel")); // NOI18N
        add(jLabel4);
        jLabel4.setBounds(90, 50, 60, 20);

        winProfit1Label.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        winProfit1Label.setText("0");
        winProfit1Label.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        winProfit1Label.setEnabled(false);
        add(winProfit1Label);
        winProfit1Label.setBounds(170, 70, 60, 20);

        jLabel8.setText(bundle.getString("win")); // NOI18N
        add(jLabel8);
        jLabel8.setBounds(170, 50, 60, 14);

        lostProfit1Label.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lostProfit1Label.setText("0");
        lostProfit1Label.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        lostProfit1Label.setEnabled(false);
        add(lostProfit1Label);
        lostProfit1Label.setBounds(250, 70, 60, 20);

        jLabel6.setText(bundle.getString("lostlabel")); // NOI18N
        add(jLabel6);
        jLabel6.setBounds(250, 50, 60, 14);

        jLabel7.setText(bundle.getString("totalLabel")); // NOI18N
        add(jLabel7);
        jLabel7.setBounds(130, 160, 40, 14);

        total1Label.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        total1Label.setText("0");
        total1Label.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        total1Label.setEnabled(false);
        add(total1Label);
        total1Label.setBounds(170, 160, 60, 20);

        warningText = bundle.getString("warningLabel");
        warningLabel.setFont(new java.awt.Font("Dialog", 0, 14)); // NOI18N
        warningLabel.setForeground(new java.awt.Color(255, 51, 51));
        warningLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        warningLabel.setText(bundle.getString("warningLabel")); // NOI18N
        add(warningLabel);
        warningLabel.setBounds(140, 190, 59, 20);
    }// </editor-fold>//GEN-END:initComponents

    private void profitSpinnerStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_profitSpinnerStateChanged
        // TODO add your handling code here:
        calculate();
    }//GEN-LAST:event_profitSpinnerStateChanged

    private void odd1SpinnerStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_odd1SpinnerStateChanged
        // TODO add your handling code here:
        calculate();
    }//GEN-LAST:event_odd1SpinnerStateChanged

    private void stake1SpinnerStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_stake1SpinnerStateChanged
        // TODO add your handling code here:
        calculate();
    }//GEN-LAST:event_stake1SpinnerStateChanged
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel lostProfit1Label;
    private javax.swing.JLabel lostProfit2Label;
    private javax.swing.JSpinner odd1Spinner;
    private javax.swing.JLabel odd2Label;
    private javax.swing.JSpinner profitSpinner;
    private javax.swing.JSpinner stake1Spinner;
    private javax.swing.JLabel stake2Label;
    private javax.swing.JLabel total1Label;
    private javax.swing.JLabel total2Label;
    private javax.swing.JLabel warningLabel;
    private javax.swing.JLabel winProfit1Label;
    private javax.swing.JLabel winProfit2Label;
    // End of variables declaration//GEN-END:variables
    
     betCalc calc;
     String warningText;
    
     private void calculate()
    {
        float Oi = Float.parseFloat(odd1Spinner.getValue().toString());
        float Si = Float.parseFloat(stake1Spinner.getValue().toString());
        float Pt = Float.parseFloat(profitSpinner.getValue().toString());
        float lost1;
        calc = new betCalc(Oi, Si, Pt, TCalc.BACKLAY);
        if (calc.isCorrect())
        {
            odd2Label.setText("" + calc.getOddFinal());
            stake2Label.setText("" + calc.getStakeFinal());
            winProfit1Label.setText("" + calc.getProfitInitial() );
            lost1 =  ((calc.getOddFinal()-1)*calc.getStakeFinal());
            lost1 = Math.round(lost1*100)/(float)100;
            lostProfit1Label.setText("" + (-lost1) );
            winProfit2Label.setText("" + calc.getProfitFinal() );
            lostProfit2Label.setText("" + (-calc.getStackeInitial()) );

            float total1 =  Math.round((calc.getProfitInitial() - lost1)*100)/(float)100;
            float total2 =  Math.round((calc.getProfitFinal() - calc.getStackeInitial())*100)/(float)100;
            total1Label.setText("" + total1);
            total2Label.setText("" +total2);
            warningLabel.setText("");
            enabledComponent(true);
            return ;
        }
        enabledComponent(false);
        warningLabel.setText(warningText);
    }

    private void enabledComponent(boolean b) {
        lostProfit1Label.setEnabled(b);
        lostProfit2Label.setEnabled(b);
        odd2Label.setEnabled(b);
        stake2Label.setEnabled(b);
        total1Label.setEnabled(b);
        total2Label.setEnabled(b);
        winProfit1Label.setEnabled(b);
        winProfit2Label.setEnabled(b);
    }
    
}
